package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.FaultCustForm;
import com.ruoyi.system.domain.WorkFault;
import com.ruoyi.system.domain.WorkPhoto;
import com.ruoyi.system.service.IWorkFaultService;
import com.ruoyi.system.service.IWorkPhotoService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WorkForm;
import com.ruoyi.system.service.IWorkFormService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单表Controller
 *
 * @author ruoyi
 * @date 2020-04-20
 */
@Controller
@RequestMapping("/system/form")
public class WorkFormController extends BaseController {
    private String prefix = "system/form";

    @Autowired
    private IWorkFormService workFormService;


    //@RequiresPermissions("system:form:view")
    @GetMapping()
    public String form() {
        return prefix + "/form";
    }

    /**
     * 查询工单表列表
     */
    //@RequiresPermissions("system:form:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkForm workForm ) {
        startPage();
        //List<WorkForm> list = workFormService.selectWorkFormList(workForm);
        String loginName = ShiroUtils.getLoginName();
        workForm.setWorkStatus("0");
        List<FaultCustForm> list = workFormService.selectWorkFormListAll(workForm,loginName);

        return getDataTable(list);
    }

    /**
     * 导出工单表列表
     */
   // @RequiresPermissions("system:form:export")
    @Log(title = "工单表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkForm workForm) {
        List<WorkForm> list = workFormService.selectWorkFormList(workForm);
        ExcelUtil<WorkForm> util = new ExcelUtil<WorkForm>(WorkForm.class);
        return util.exportExcel(list, "form");
    }

    /**
     * 新增工单表
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存工单表
     */
    //@RequiresPermissions("system:form:add")
    @Log(title = "工单表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkForm workForm) {
        return toAjax(workFormService.insertWorkForm(workForm));
    }

    /**
     * 修改工单表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        WorkForm workForm = workFormService.selectWorkFormById(id);
        mmap.put("workForm", workForm);
        return prefix + "/edit";
    }

    /**
     * 修改保存工单表
     */
    //@RequiresPermissions("system:form:edit")
    @Log(title = "工单表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkForm workForm) {
        return toAjax(workFormService.updateWorkForm(workForm));
    }

    /**
     * 删除工单表
     */
    //@RequiresPermissions("system:form:remove")
    @Log(title = "工单表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(workFormService.deleteWorkFormByIds(ids));
    }

    /**
     * 提交审核
     */
    //@RequiresPermissions("system:form:remove")
    @Log(title = "工单表", businessType = BusinessType.check)
    @PostMapping("/check/{id}")
    @ResponseBody
    public AjaxResult check(@PathVariable("id") Long id) {

        return toAjax(workFormService.updataWorkFormById(id));
    }

}
